﻿using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using Common;
using IBll;

namespace Controllers.Filter
{
    /// <summary>
    /// 设备验证
    /// </summary>
    public class EquipmentAuthorizeAttribute:AuthorizeAttribute
    {
        public IBllSession CurrentBllSession { get; set; }

        public EquipmentAuthorizeAttribute()
        {
            CurrentBllSession = IocHelper.AutofacResolve<IBllSession>();
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            HttpContextBase httpContext = actionContext.Request.Properties["MS_HttpContext"] as HttpContextBase;
            HttpRequestBase httpRequest = httpContext.Request;
            string equipmentCode = httpRequest["EquipmentCode"];//获取请求参数对应的值

            if(!string.IsNullOrEmpty(equipmentCode))
            {
                if (true)//此处写验证业务逻辑
                {
                    base.IsAuthorized(actionContext);
                }
                else
                {
                    HandleUnauthorizedRequest(actionContext);
                }
            }
            else
            {
                HandleUnauthorizedRequest(actionContext);
            }
        }
    }
}